<?php

namespace App\Http\Middleware;

use DB;
use Closure;
use Carbon\Carbon;

class StewardClient
{

    private $id;

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $request_data = $request->all();

        $this->addLog($request->path(), $request_data);

        if ($this->apiKeyCheck($request_data)) {
            return $next($request);
        }

        if (!isset($request_data['appId']) || empty($request_data['appId'])) {
            $return_data = [
                'code' => 100000 ,
                'message' => '缺少参数：appId'
            ];
            $this->updateLog($return_data);
            return response()->json($return_data);
        }

        if (!isset($request_data['params'])) {
            $return_data = [
                'code' => 100000 ,
                'message' => '缺少参数：params'
            ];
            $this->updateLog($return_data);
            return response()->json($return_data);
        }

        if (!isset($request_data['sign']) || empty($request_data['sign'])) {
            $return_data = [
                'code' => 100000 ,
                'message' => '缺少参数：sign'
            ];
            $this->updateLog($return_data);
            return response()->json($return_data);
        }

        $api_data = [
            'dcf7da0be34211e4882200163e00313a' => '5d6011fc3373d26cbc7916ff41878925'
        ];

        if (!isset($api_data[$request_data['appId']])) {
            $return_data = [
                'code' => 100000 ,
                'message' => 'appId参数错误'
            ];
            $this->updateLog($return_data);
            return response()->json($return_data);
        }

        //生成签名
        $sign = $this->createSign($request_data, $api_data[$request_data['appId']]);
     
        if ($sign != $request_data['sign']) {
            $return_data = [
                'code' => 100001 ,
                'message' => '数据加密验签失败，服务器签名：' . $sign
            ];
            $this->updateLog($return_data);
            return response()->json($return_data);
        }

        $params_data = json_decode($request_data['params'], true);
        if (!is_array($params_data) || empty($params_data)) {
            $params_data = [];
        }

        foreach($params_data as $key=>$value) {
            $request->offsetSet($key, $value);
        }

        $response = $next($request);

        if (method_exists($response, 'getData')) {
            $this->updateLog($response->getData(true));
        } else {
            $this->updateLog('程序执行出错');
        }

        return $response;

    }

    /**
     * 接口验签
     * @param $data
     * @param $key
     * @return string
     */
    private function createSign($data, $key) {

        ksort($data);

        $sign_str = '';
        foreach ($data as $k => $v) {
            if ($v === '' || $k == 'sign' || $k == '_url' || $k == 'file' || $k == 'fileKey') {
                continue;
            }
            $sign_str .= $k . '=' . $v;
        }

        $sign_str .= $key;

        return strtoupper(md5($sign_str));

    }

    /**
     * 添加接口请求数据
     * @param string $api_name 接口名称
     * @param array $request_data 请求数据数组
     * @return mixed
     */
    private function addLog($api_name, $request_data) {


    }

    /**
     * 更新接口返回数据
     * @param array $return_data 返回数据数组
     * @return bool
     */
    private function updateLog($return_data) {


    }

    /**
     * 验证内部测试密钥
     * @param $request
     * @return bool
     */
    private function apiKeyCheck($request) {

        if (!isset($request['e_secret_key']) || empty($request['e_secret_key'])) {
            return false;
        }
        $api_key = file_get_contents('http://www.ebsig.com/key/api');
        $api_key = json_decode($api_key);
        if ($request['e_secret_key'] != $api_key) {
            return false;
        }
        return true;

    }


}

